<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of fb
 *
 * @author Hallucinogen
 */
class FbModel {
    private static $app_id = "160919723966419";
    private static $app_secret = "3d7680adc44e299d8084d2f5d03b2703";

    public function postToWall($message, $target_id = null) {
        $message = urlencode($message);
        $access_token_url = "https://www.facebook.com/dialog/oauth";
        $parameters = "scope=publish_stream&client_id=" . FbModel::$app_id . "&client_secret=" . FbModel::$app_secret . "&redirect_uri=" . urlencode(FbModel::currentPageURL());
        $code = $_REQUEST["code"];

        if (empty($code)){
            echo("<script> top.location.href='" . $access_token_url ."?" . $parameters . "'</script>");
        }

        $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
        . FbModel::$app_id . "&redirect_uri=" . urlencode(FbModel::currentPageURL()) . "&client_secret="
        . FbModel::$app_secret . "&code=" . $code;

        if ($target_id != null)
            $token_url .= "&target_id=" . $target_id;
        
	$access_token = file_get_contents($token_url);

	$graph_url = "https://api.facebook.com/method/stream.publish?" . $access_token . "&message=" . $message;
	$post = file_get_contents($graph_url);
    }

    public static function currentPageURL() {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"] .$_SERVER["PHP_SELF"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"] .$_SERVER["PHP_SELF"];
        }
        return $pageURL;

        //return "http://216.18.206.54/~gogo/progin/";
    }
}
?>
